const app = getApp();
const baseUrl = "https://yps.yikesong.cc/api"; // yikesong.cc正式服
// const baseUrl = "https://peter.xiaomiqiu.com/api"; // 小米球测试服
// const baseUrl = "https://hc.guyubao.com/api";
const request = function(createData) {
  return new Promise(function(resolve, reject) {
    //console.log('走接口数据了',createData);
    let isJsonrequest = false;
    let isPathParams = false;
    //判断json请求
    if (createData.data != undefined) {
      isJsonrequest = true;
    }
    //判断?连接请求
    let params = '?';
    if (createData.params != undefined) {
      isPathParams = true;
      for (let i in createData.params) {
        params += i;
        params += '=';
        params += createData.params[i];
        params += '&';
      }
      //获取最后一个&
      params = params.substring(0, params.length - 1);
    }

    //判断头信息
    let headers = {
      Accept: 'application/json',
      // "Content-Type": "json",
      Authorization: 'Bearer ' + wx.getStorageSync("third_Session").accessToken,
    };


    if (createData.headers != null) {
      headers = createData.headers;
    }
    // console.log(`${baseUrl + createData.url}` + (isPathParams ? params : ''))
    wx.request({
      url: `${baseUrl + createData.url}` + (isPathParams ? params : ''),
      data: JSON.stringify(createData.data),
      header: headers,
      method: createData.method,
      success: function(res) {
        // console.log('====================',res)
        // console.log('res.data.flag===',res.data.flag)
        if (res.data.flag) {
          resolve(res.data);
        } else {
          let title;
          if (res.data.code === 2403) { //授权失败
            // 进行授权操作
            wx.showModal({
              title: '微信授权',
              content: '该页面需要获取您的昵称等公开信息，未检测到相关信息，是否跳转授权页面进行重新授权？',
              showCancel: true,
              cancelText: '再等等',
              cancelColor: '#e6e6e6',
              confirmText: '授权',
              confirmColor: '#ec701e',
              success: function(res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '/pages/logs/logs',
                  })
                } else if (res.cancel) {
                  wx.navigateBack({
                    delta: 1,
                    fail: function(res) {
                      wx.switchTab({
                        url: '/pages/index/index',
                      })
                    }
                  })
                }
              },
              fail: function(res) {},
              complete: function(res) {},
            })
          } else {
            title = res.data.msg;
            console.log(res)
            //console.log(res.data)
            if (res.data.msg == "该优惠券领取已达上限啦,请明日再来吧~v~") {
              wx.showToast({
                title: '优惠券已达上限',
                icon: "none",
                image: "/img/ku.png",
                duration: 2000
              })
              return;
            }
            console.log('title', title)
            wx.showToast({
              title: title,
              // title:'授权成功',
              icon: 'none',
              duration: 2000,
              mask: false,
              success: function(res) {},
              fail: function(res) {},
              complete: function(res) {},
            })
          }
          reject(new Error(res.message || 'Error'))
        }
      },
      fail: function(res) {
        reject(res);
      },
      complete: function(res) {},
    })
  });
}


export default request